import { defineStore } from 'pinia'
import { ref } from 'vue'
import { promotionApi } from '@/api'

export const usePromotionStore = defineStore('promotion', () => {
  const options = ref<any[]>([])

  const fetchOptions = async () => {
    if (options.value.length > 0) {
      return options.value
    } else {
      options.value = await getProcessedData()
      return options.value
    }
  }
  const getProcessedData = async () => {
    try {
      const {
        data: { list },
      } = await promotionApi.getPromotionList({ is_all: true })

      const processedData = list.map((item: any) => ({
        label: item.title,
        value: String(item.id),
        key: item.id,
      }))
      processedData.unshift({
        label: '全部',
        value: '',
        key: '',
      })
      return processedData
    } catch (error) {
      console.error('獲取全部推廣活動數據失敗:', error)
    }
  }

  return {
    options,
    fetchOptions,
  }
})
